from app.aoduo_admin.models import AdminGroup, AuthType, AdminAccount
from rest_framework.views import APIView
from django.http import JsonResponse
from common.authCheck.adminAuth import check_admin_login
from common.dictInfo import model_to_dict
from .chiefAdminAuthVerify import chiefAdminCheck
import json


class AdminGroupInfoView(APIView):

    @check_admin_login
    @chiefAdminCheck
    def get(self, request, gid):
        """
        获取用户组信息
        :param request:
        :param gid:
        :return:
        """
        group = AdminGroup.objects.filter(id=gid)
        if not group.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '管理组不存在'
            }, status=404)
        group = group[0]
        groupDict = model_to_dict(group, fields=['group_name', 'create_time'])
        authDict = [model_to_dict(auth) for auth in group.auth.all()]
        groupAdminDict = [model_to_dict(admin, fields=['username', 'id']) for admin in group.group_admin.all()]
        return JsonResponse({
            'status': True,
            'group': groupDict,
            'auth': authDict,
            'group_admin': groupAdminDict
        })

    @check_admin_login
    @chiefAdminCheck
    def post(self, request):
        """
        新建用户组
        :param request:
        :return:
        """
        jsonParams = json.loads((request.body).decode('utf-8'))
        auth = AuthType.objects.filter(id__in=jsonParams.get('authID'))
        newGroup = AdminGroup.objects.create(group_name=jsonParams.get('group_name'))
        for aid in auth:
            # 添加权限到用户组内
            newGroup.auth.add(aid)
        return JsonResponse({
            'status': True,
            'newID': newGroup.id
        })

    @check_admin_login
    @chiefAdminCheck
    def put(self, request, gid):
        """
        修改组信息/添加组成员
        :param request:
        :param gid:
        :return:
        """
        try:
            jsonParams = json.loads((request.body).decode('utf-8'))
            group = AdminGroup.objects.filter(id=gid)
            if not group.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '管理组不存在'
                }, status=404)
            group = group[0]
            if jsonParams.get('addAuth') is not None:
                addAuth = AuthType.objects.filter(id__in=jsonParams.get('addAuth'))
                for auth in addAuth:
                    group.auth.add(auth)
            if jsonParams.get('delAuth') is not None:
                delAuth = AuthType.objects.filter(id__in=jsonParams.get("delAuth"))
                for auth in delAuth:
                    group.auth.remove(auth)
            if jsonParams.get('addAdmin') is not None:
                addAdmin = AdminAccount.objects.filter(id__in=jsonParams.get('addAdmin'))
                for admin in addAdmin:
                    group.group_admin.add(admin)
            if jsonParams.get('delAdmin') is not None:
                delAdmin = AdminAccount.objects.filter(id__in=jsonParams.get('addAdmin'))
                for admin in delAdmin:
                    group.group_admin.remove(admin)
            return JsonResponse({
                'status': True,
                'group_name': group.group_name,
                'id': group.id
            })
        except Exception as ex:
            return JsonResponse({
                'status': False,
                'errMsg': str(ex)
            })

    @check_admin_login
    @chiefAdminCheck
    def delete(self, request, gid):
        """
        删除用户组
        :param request:
        :param gid:
        :return:
        """
        pass
